package org.example.sync;

import java.util.Date;

/**
 * @author zhaoPin
 * @Date 2024/9/9  17:43
 * @description: 不采用代码块方式来运行，直接采用对象锁，会发现500毫秒到了，根本没有执行完毕
 */
public class SynchronizedOldDemo {
    private static Integer count = 0;

    public static void main(String[] args) {
        final var start = new Date();

        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    // 采用对象锁方式
                    synchronized (count){
                        for (int i = 0; i < 1000000; i++) {
                            count++;
                        }
                    }
                }
            });
            thread.start(); // 启动线程
        }

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        final var end = new Date();
        final var l = end.getTime() - start.getTime();
        System.out.println("result: " + count + " time:" + l);
    }


}
